//
//  TouchExampleViewController.m
//  TouchExample
//

#import "TouchExampleViewController.h"

@implementation TouchExampleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	
	float boxSize = 100.0;
	CGRect redBoxRect = CGRectMake(0,180,boxSize,boxSize);
	redBox = [[UIView alloc] initWithFrame:redBoxRect];
	redBox.backgroundColor = [UIColor redColor];
	
	CGRect blueBoxRect = CGRectMake(110,180,boxSize,boxSize);
	blueBox = [[UIView alloc] initWithFrame:blueBoxRect];
	blueBox.backgroundColor = [UIColor blueColor];

	CGRect greenBoxRect = CGRectMake(220,180,boxSize,boxSize);
	greenBox = [[UIView alloc] initWithFrame:greenBoxRect];
	greenBox.backgroundColor = [UIColor greenColor];
	
	[self.view addSubview:redBox];
	[self.view addSubview:blueBox];
	[self.view addSubview:greenBox];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
	CGPoint currentPoint = [touch locationInView:self.view];
	
	if (touch.view != self.view)
		[touch view].center = currentPoint;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
	if (touch.view != self.view) {
		[self.view bringSubviewToFront:touch.view];
		
		float zoom = -25.0;
		CGRect newRect = CGRectInset([touch.view frame], zoom, zoom);
		
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.2];
		[touch.view setFrame:newRect];
		[UIView commitAnimations];
	}
	
	NSLog(@"tap count: %i", [touch tapCount]);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
	if (touch.view != self.view) {
		
		float zoom = 25.0;
		CGRect newRect = CGRectInset([touch.view frame], zoom, zoom);
		
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.2];
		[touch.view setFrame:newRect];
		[UIView commitAnimations];
	}
}

- (void)dealloc {
	[redBox release];
	[blueBox release];
	[greenBox release];
	
    [super dealloc];
}

@end
